package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsGoodsSpecificationMapper;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.GoodsSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodsSpecificationServiceImpl implements GoodsSpecificationService {
    @Autowired
    private DtsGoodsSpecificationMapper dtsGoodsSpecificationMapper;

    @Override
    public List<DtsGoodsSpecification> findListByGoodsId(Integer id) {
        return dtsGoodsSpecificationMapper.selectList(new QueryWrapper<DtsGoodsSpecification>()
                .eq("goods_id",id)
                .eq("deleted",false));
    }

    @Override
    public void update(DtsGoodsSpecification[] specifications, Integer id) {
        dtsGoodsSpecificationMapper.updateByGoodsId(id);
        if (specifications==null||specifications.length==0){
            return;
        }
        for (DtsGoodsSpecification specification : specifications) {
            specification.setUpdateTime(new Date());
            specification.setDeleted(false);
            if (specification.getId()!=null){
                dtsGoodsSpecificationMapper.updateById(specification);
            }else{
                specification.setAddTime(new Date());
                specification.setGoodsId(id);
                dtsGoodsSpecificationMapper.insert(specification);
            }
        }
    }

    @Override
    public void create(DtsGoodsSpecification[] specifications, Integer goodsId) {
        if (specifications==null||specifications.length==0){
            return;
        }
        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(goodsId);
            specification.setDeleted(false);
            specification.setAddTime(new Date());
            specification.setUpdateTime(new Date());
            dtsGoodsSpecificationMapper.insert(specification);
        }

    }
}
